package sg.edu.nus.iss.billing.service.customer;

import java.util.List;

import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.BadDaoCallException;
import sg.edu.nus.iss.billing.common.exception.BadDbCallException;
import sg.edu.nus.iss.billing.dao.customer.CustomerDAO;
import sg.edu.nus.iss.billing.dao.customer.ICustomerDAO;
import sg.edu.nus.iss.billing.exception.customer.InvalidCustomerDataException;
import sg.edu.nus.iss.billing.service.account.AccountImpl;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.customer.AddressVO;
import sg.edu.nus.iss.billing.vo.customer.CustomerVO;

public class CustomerServiceImpl implements ICustomerService{

	private ICustomerDAO customerDAO;
	private String logImpl = "CLASSNAME CustomerServiceImpl :  ";
	
	public CustomerServiceImpl(){
		printLog(logImpl + "Constructor initialized");
		customerDAO = new CustomerDAO();
		printLog(logImpl + "Constructor initialized DONE");
	}
	
	private void printLog(String str){
		System.out.println(str);
	}
	
	public String  registerCustomer(CustomerVO customerVo) throws InvalidCustomerDataException, BadDaoCallException {
		String acctNo = null;
		logImpl += " registerCustomer() CUSTOMER NRIC :   "+customerVo.getNric() +" : ";
		
		printLog(logImpl + "..Check if the VO has NRIC set ");
		
		if(customerVo!=null && (customerVo.getNric() == null || customerVo.getNric().equalsIgnoreCase(""))){
			throw new InvalidCustomerDataException("CUSTOMER NRIC IS NULL or BLANK ");
		}
		
		printLog(logImpl + "..Checking if it is existing customer ");
		boolean proceed = false;
		//customerVo.setAccount(account)
		CustomerVO checkCustomer = getCustomer(customerVo);
//		printLog(logImpl + "..Checking if it is existing customer ");
		printLog(logImpl + "CustomerVO checkCustomer:" +  checkCustomer);
		
		if(checkCustomer == null){
			acctNo = "ACCT-"+Util.generateRandomNumer();
			printLog(logImpl + "CustomerVO Account Number :" +  acctNo);
				AccountVO account = new AccountVO();
				account.setAccountNo(acctNo);
				account.setNric(customerVo.getNric());
				customerVo.setAccount(account);
				AddressVO tempAddrVO = customerVo.getAddress();
				tempAddrVO.setAddresseeNric(customerVo.getNric());
				customerVo.setAddress(tempAddrVO);
			Object obj = customerDAO.create(customerVo);
				if(obj == null){
					printLog(logImpl + "CustomerVO   : Return Object is NULL while creating Customer" +  acctNo);
					throw new InvalidCustomerDataException("CUSTOMER NOT CREATED ");
				}
				printLog(logImpl + "CustomerVO   : Begining to create account " +  acctNo);
				AccountImpl actImpl = new AccountImpl();
				try {
					actImpl.createAccount(account);
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
		return acctNo;
	}

	public boolean updateCustomerDetail(CustomerVO customerVO){
		
		int change = customerDAO.update(customerVO);
		return (change==1?true:false);
	}


	public CustomerVO getCustomer(CustomerVO customerVO) {
		
		String nric = customerVO.getNric().trim();
		customerDAO = new CustomerDAO();
		CustomerVO c = customerDAO.findCustomerByNric(nric);
		System.out.println(c);
		return c;
	}

	public CustomerVO getCustomerByNric(String nric){
		logImpl += " registerCustomer() CUSTOMER NRIC :   "+nric +" : ";
		printLog(logImpl);
		CustomerVO c = customerDAO.findCustomerByNric(nric.trim());
		if(c == null){
			
		}
		return c;
	}
	public List<CustomerVO> findAllCustomers(){
		List<CustomerVO> list = customerDAO.findAllCustomers();
		return list;
	}
	

	@Override
	public CustomerVO getCustomerByAcc(AccountVO acc) {
		
		String accNo = acc.getAccountNo().trim();
		CustomerVO c = customerDAO.findCustomerByAcc(accNo);
		
		AccountImpl aimpl = new AccountImpl();
		AccountVO avo = aimpl.getAccountDetailsByAccountNumber(accNo);
		if (avo != null)
		{
		c.getAccount().setStatus(avo.getStatus());
		if(c.getAccount().getAccountNo().equals(accNo))
			return c;
		}
		
		return null;
	}

	@Override
	public List<CustomerVO> getAllCustomers() {
		return customerDAO.findAllCustomers();
		
		
	}
}
